//返回ID对象
var g = function(id){
	return document.getElementById(id);
}

//为字符串增加trim方法，使用正则表达式截取空格
String.prototype.trim = function(){							
	return this.replace(/^\s*/,"").replace(/\s*$/,"");
}

//验证的ID
var cida = g("cida");
var cname = g("cname");
var cno = g("cno");
var cvin = g("cvin");
var cengine = g("cengine");
var ctype = g("ctype");

//错误提示
var cidamsg = g("cidamsg");
var cnamemsg = g("cnamemsg");
var cnomsg = g("cnomsg");
var cvinmsg = g("cvinmsg");
var cenginemsg = g("cenginemsg");
var ctypemsg = g("ctypemsg");
var usernamemsg = g("usernamemsg");

//客户验证
function cidaCheck(){
	if(cida.value.trim() == "" || cida.value == null){
		cida.style.border = "1px solid";
		cida.style.borderColor = "red";
		cidamsg.innerHTML = "<font color='red' size='1px'>请填写客户名称！</font>";
		return false;
	}else if(usernamemsg.value == "不存在"){
		cida.style.border = "1px solid";
		cida.style.borderColor = "red";
		cidamsg.innerHTML = "<font color='red' size='1px'>该客户不存在，请您核对后重新输入！</font>";
		return false;
	}else{
		cida.style.border = "";
		cida.style.borderColor = "";
		cidamsg.innerHTML = "";
		return true;
	}
}

//客户校验
cida.onblur = function(){
	customerExits();	
	cidaCheck();
}

//汽车名称验证
function cnameCheck(){
	if(cname.value.trim() == "" || cname.value == null){
		cname.style.border = "1px solid";
		cname.style.borderColor = "red";
		cnamemsg.innerHTML = "<font color='red' size='1px'>请填写汽车名称！</font>";
		return false;	
	}else{
		cname.style.border = "";
		cname.style.borderColor = "";
		cnamemsg.innerHTML = "";
		return true;
	}
}

//车牌号码验证
function cnoCheck(){
	cnoRegex = /^[\u4E00-\u9FA5]{1}[A-Z]{1}[A-Z0-9]{5}$/;
	
	if(cno.value.trim() == "" || cno.value == null){
		cno.style.border = "1px solid";
		cno.style.borderColor = "red";
		cnomsg.innerHTML = "<font color='red' size='1px'>请填写车牌号码！</font>";
		return false;	
	}else if(!cnoRegex.test(cno.value)){
		cno.style.border = "1px solid";
		cno.style.borderColor = "red";
		cnomsg.innerHTML = "<font color='red' size='1px'>您填写的车牌号码格式不对！请核对后重新填写~</font>";
		return false;
	}else{
		cno.style.border = "";
		cno.style.borderColor = "";
		cnomsg.innerHTML = "";
		return true;
	}
}

//车架号验证
function cvinCheck(){
	cvinRegex = /^[a-zA-Z0-9]{17}$/;
	
	if(cvin.value.trim() == "" || cvin.value == null){
		cvin.style.border = "1px solid";
		cvin.style.borderColor = "red";
		cvinmsg.innerHTML = "<font color='red' size='1px'>请填写车架号！</font>";
		return false;	
	}else if(!cvinRegex.test(cvin.value)){
		cvin.style.border = "1px solid";
		cvin.style.borderColor = "red";
		cvinmsg.innerHTML = "<font color='red' size='1px'>您填写的车架号格式不对！请核对后重新填写~</font>";
		return false;
	}else{
		cvin.style.border = "";
		cvin.style.borderColor = "";
		cvinmsg.innerHTML = "";
		return true;
	}
}

//发动机号验证
function cengineCheck(){
	cengineRegex = /^[a-zA-Z0-9]{8}$/;
	
	if(cengine.value.trim() == "" || cengine.value == null){
		cengine.style.border = "1px solid";
		cengine.style.borderColor = "red";
		cenginemsg.innerHTML = "<font color='red' size='1px'>请填写发动机号！</font>";
		return false;	
	}else if(!cengineRegex.test(cengine.value)){
		cengine.style.border = "1px solid";
		cengine.style.borderColor = "red";
		cenginemsg.innerHTML = "<font color='red' size='1px'>您填写的发动机号格式不对！请核对后重新填写~</font>";
		return false;
	}else{
		cengine.style.border = "";
		cengine.style.borderColor = "";
		cenginemsg.innerHTML = "";
		return true;
	}
}

//汽车类型验证
function ctypeCheck(){		
	if(ctype.value == "--车辆类型--"){	
		ctype.style.border = "1px solid";
		ctype.style.borderColor = "red";
		ctypemsg.innerHTML = '<font color="red" size="2px">请选择车辆类型</font>';
		return false;					
	}else{
		ctype.style.border = "";
		ctypemsg.innerHTML = '<font color="green" size="2px">√</font>';
		return true;
	}
}

function checkAll(){
	return cidaCheck() && cnameCheck() && cnoCheck() && cvinCheck() && cengineCheck() && ctypeCheck();
}

function checkAll2(){
	return cidaCheck() || cnameCheck() || cnoCheck() || cvinCheck() || cengineCheck() || ctypeCheck();
}